package com.niit.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.niit.model.Book;
import com.niit.service.BookService;
import com.niit.service.impl.BookServiceImpl;

/**
 * Servlet implementation class ListBookServlet
 */
@WebServlet("/BookListServlet.do")
public class BookListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookListServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("书籍列表");
		BookService bookService = new BookServiceImpl();
		List<Book> lstBooks = bookService.queryBooks();
		// 将查询出的书籍数据保存到session中
		HttpSession session = request.getSession();
		session.setAttribute("lstBooks", lstBooks);
		// 重定向到booklist.jsp页面
		response.sendRedirect("pages/booklist.jsp");
//		request.setAttribute("lstBooks", lstBooks);
//		request.getRequestDispatcher("pages/book.jsp").forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
